package com.example.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.Service.RedisService;
import com.example.fanqie.entity.Author;
import com.example.fanqie.entity.Category;
import com.example.fanqie.entity.Chapter;
import com.example.fanqie.entity.Novel;
import com.example.fanqie.mapper.AuthorMapper;
import com.example.fanqie.mapper.CategoryMapper;
import com.example.fanqie.mapper.ChapterMapper;
import com.example.fanqie.mapper.NovelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/novel")
public class NovelDao {


    @Resource
    private NovelMapper   novelMapper;

    @Resource
    private AuthorMapper authorMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @Autowired
    private RedisService redisService;

    @RequestMapping("/selectlist")
    public Object selectlist(@RequestBody  Novel novel ){
        //最新产品
        IPage<Novel> page= new Page<>(1,10);

        //排序
        page.orders().add(OrderItem.desc("novel_id"));
        QueryWrapper<Novel> queryWrapper = new QueryWrapper<>();

        if(novel.getCategoryId()!=null){
            queryWrapper.eq("category_id",novel.getCategoryId());
        }
        if(novel.getSex()!=null){
            queryWrapper.eq("sex",novel.getSex());
        }
        if(novel.getStatu()!=null){
            queryWrapper.like("statu",novel.getStatu());
        }
        queryWrapper.eq("dlt", 1);
        IPage<Novel> iPage = novelMapper.selectPage(page,   queryWrapper );
        List<Novel> news = iPage.getRecords();

        for (Novel n : news) {

            Author author = authorMapper.selectauthorId(n.getAuthorId());
            Category category = categoryMapper.selectcategoryId(n.getCategoryId());
            n.setAuthorName(author.getAuthorName());
            n.setCategoryName(category.getCategoryName());
        }


        Map<String,Object> ret = new HashMap<>();
        ret.put("news",news);

        redisService.saveToRedis(ret, "novellist");
        return ret;

//        try {
//
//            // 模拟在服务中发生异常
//            throw new RuntimeException("Simulated exception");
//
//
//        } catch (Exception e) {
//            // 捕获异常并返回异常信息
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred: " + e.getMessage());
//        }




    }


    @RequestMapping("/selectId")
    public Object selectId(@RequestBody  Novel novel){
        Novel novel1 = novelMapper.selectnovelId(novel.getNovelId());
        Category category = categoryMapper.selectcategoryId(novel1.getCategoryId());
        novel1.setCategoryName(category.getCategoryName());

        return novel1;
    }


    @RequestMapping("/selectnovelId")
    public Object selectnovelId(@RequestParam(value = "novelId") Integer novelId){

        Novel novel = novelMapper.selectnovelId(novelId);

        return novel.getTitle();
    }

    @RequestMapping("updataMoney")
    public int updataMoney(@RequestParam(value = "money") Float money,@RequestParam(value = "novelId") Integer novelId){

        Novel novel = novelMapper.selectnovelId(novelId);
        float x = novel.getNMoney() + money;
        novel.setNMoney(x);
        int i = novelMapper.updateById(novel);
        return i;
    }

    //查询作者小说列表
    @RequestMapping("/queryNovelList")
    public List<Novel> queryNovelList(@RequestParam("authorId") Integer authorId){

        List<Novel> novelList = novelMapper.selectNovel(authorId);
        for (Novel n : novelList) {

            Category category = categoryMapper.selectcategoryId(n.getCategoryId());
            n.setCategoryName(category.getCategoryName());
            List<Chapter> selectchapterlist = chapterMapper.selectchapterlist(n.getNovelId());
            if(selectchapterlist.size() > 0){
                n.setChapterTitle(selectchapterlist.get(selectchapterlist.size() - 1).getChapterTitle());
                n.setPublishDatezuixin(selectchapterlist.get(selectchapterlist.size() - 1).getPublishDate());
                n.setChapterId(selectchapterlist.get(selectchapterlist.size() - 1).getChapterId());
                AtomicInteger x = new AtomicInteger(0);
                selectchapterlist.forEach(chapter -> {
                    x.addAndGet(chapter.getCount());
                });
                n.setWordCount(x.get());
            }


        }
        return novelList;

    }


}
